import './App.css'
import {createBrowserRouter, RouterProvider} from "react-router-dom";
import Login from "@/pages/login";
import {allMenus} from "@/services/menu";
import {currentUser} from "@/services/login";
import {useRequest} from "ahooks";
import Admin from "@/pages/admin";
import SocialCallbackLogin from "@/pages/login/SocialCallbackLogin";
import SchemaEditor from "@/pages/schemaeditor";
import UserLayout from "@/components/Layout/UserLayout";
import ResultPage from "@/components/ResultPage";
import {NotFound} from "amis";
import {PageLoading} from "@ant-design/pro-layout";
import {createStore, Provider} from "jotai";
import RequireAuth from "@/components/Layout/RequireAuth";
import VConsole from 'vconsole';
import initialStatesAtom from "@/store/initialStatesAtom";
import Oauth from "@/pages/login/Oauth.tsx";
interface MenuItem {
    label: string;
    path?: string;
    schemaApi?: string;
    children?: MenuItem[];
}
// @ts-ignore
const loopRouteItem = (menus:MenuItem[]) => menus?.map(({ label,schemaApi, children, ...item }) => {
    return {
        ...item,
        label: label,
        children: children && loopRouteItem(children),
        element: schemaApi&&<RequireAuth><Admin schemaApi={schemaApi}/></RequireAuth>
    }
})
export const router = createBrowserRouter([
    {
        path: '/login',
        element: <Login />
    },
    {
        path: '/social-callback/:source',
        element: <SocialCallbackLogin />
    },
    {
        path: '/oauth/:platform',
        element: <RequireAuth><Oauth /></RequireAuth>
    },
    {
        path: '/schemaeditor/:id',
        element: <RequireAuth><SchemaEditor /></RequireAuth>
    },
    {
        path: '/',
        element: <UserLayout />,
        children: [
        ],
    },
    {
        path: '/result/:status/:title/:msg',
        element: <ResultPage/>,
    },
    {
        path: '*',
        element: <NotFound/>,
    }]);

//全局状态
const initialStore = createStore()
function App() {
    const query = new URLSearchParams(location.search);
    const debug = query.get('amisDebug');
    if(debug){
        new VConsole();
    }
    useRequest(allMenus,{
        onSuccess: (data) => {
            if(data.data.code!==200){
                return;
            }
            // 获取菜单后动态添加路由
            router.routes[4].children?.push(...loopRouteItem(data.data.data))
        }
    })
    const {loading} = useRequest(currentUser, {
        onSuccess: async (data) => {
            if(data.data.code===200){
                initialStore.set(initialStatesAtom, {currentUser: data.data.data,isAuthenticated:true})
            }
        }
    })


  return (
      <div
          style={{
              height: '100vh',
              overflow: 'auto',
              width: '100vw',
          }}
      >
          <Provider store={initialStore}>
                  {loading? <PageLoading/>:<RouterProvider router={router} fallbackElement={<PageLoading/>} />}
          </Provider>
      </div>
  )
}

export default App
